<template>
  <div>
    <el-dialog
      :model-value="props.addInfo?.isShow"
      :title="props.addInfo?.isAdd ? '添加管理员' : '编辑管理员'"
      center
      @close="cancel"
    >
      <el-form :model="memberForm" :rules="rules" ref="formRef">
        <el-form-item
          label="所属角色"
          :label-width="formLabelWidth"
          prop="roleid"
        >
          <el-select v-model="memberForm.roleid" placeholder="请选择">
            <!-- <el-option label="顶级菜单" :value="0" /> -->
            <el-option
              v-for="item in store.getters['role/getroleList']"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="用户名称"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="memberForm.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
                  <el-input v-model="memberForm.password" autocomplete="off" />
              </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="memberForm.status"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="confirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits, defineExpose } from "vue";
import { useStore } from "vuex";
import { ref, reactive } from "vue";
import { FormInstance, FormRules } from "element-plus";
import { useradd, useredit, userinfo } from "../../request/api";
import { ElMessageBox, ElMessage } from "element-plus";
let store = useStore();
store.dispatch("role/changeRoleAction");
let props = defineProps({
  addInfo: Object,
});
let emit = defineEmits(["cancel"]);
let formLabelWidth = ref("120px");
let formRef = ref<FormInstance>();
let memberForm = ref({
  roleid: "", //角色编号
  username: "", //管理员名称
  password: "", //密码
  status: 1, //状态   1正常2禁用   number类型number
});
let rules = reactive<FormRules>({
  username: [
    { required: true, message: "请输入用户·名称", trigger: "blur" },
    { min: 3, max: 11, message: "长度 3 到 11 ", trigger: "blur" },
  ],
  roleid: [{ required: true, message: "请输入上级管理员", trigger: "blur" }],
});
let reset = () => {
  memberForm.value = reactive({
    roleid: "", //角色编号
    username: "", //管理员名称
    password: "", //密码
    status: 1, //状态   1正常2禁用   number类型number
  });
  formRef.value?.resetFields();
};
let cancel = () => {
  emit("cancel", false);
  reset();
};
let lookup = (uid: number) => {
  userinfo({ uid }).then((res: any) => {
    if (res.code == 200) {
      memberForm.value = res.list;
      memberForm.value.uid = uid;
      memberForm.value.password = "";
    } else {
      ElMessage.error(res.msg);
    }
  });
};
let confirm = () => {
  formRef.value?.validate((val) => {
    if (val) {
      if (props.addInfo?.isAdd) {
        useradd(memberForm.value).then((res: any) => {
          if (res.code == 200) {
            ElMessage.success(res.msg);
            cancel();
            store.dispatch("member/getUserListAction");
            store.dispatch('member/getCountAction');
          } else {
            ElMessage.error(res.msg);
          }
        });
      } else {
        useredit(memberForm.value).then((res: any) => {
          if (res.code == 200) {
            ElMessage.success(res.msg);
            cancel();
            store.dispatch("member/getUserListAction");
            store.dispatch('member/getCountAction');
          } else {
            ElMessage.error(res.msg);
          }
        });
      }
    }
  });
};
defineExpose({
  lookup,
});
</script>

<style scoped>
</style>